// import { JSFuncs, Boards } from 'mixly'
import * as Blockly from 'blockly/core'

// Sentry3
export const Sentry3Begin = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_mode_obj = this.getFieldValue('mode_obj')
    var dropdown_addr_obj = this.getFieldValue('addr_obj')

    generator.definitions_['include_Sentry'] = '#include <Sentry.h>'
    generator.definitions_[
        `var_declare_Sentry3`
    ] = `Sentry3 sentry3_${dropdown_sentry_index}(${dropdown_addr_obj});`

    if (dropdown_mode_obj == 'Wire') {
        generator.definitions_['include_Wire'] = '#include <Wire.h>'
        generator.setups_['setup_Wire'] = 'Wire.begin();\n'
    }

    var code = `while (SENTRY_OK != sentry3_${dropdown_sentry_index}.begin(&${dropdown_mode_obj})) {yield();}\n`

    return code
}

export const Sentry3VisionSetStatus = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_VisionStatus = this.getFieldValue('VisionStatus')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')

    return `sentry3_${dropdown_sentry_index}.Vision${dropdown_VisionStatus}(${dropdown_vision_obj});\n`
}

export const Sentry3VisionSetParamNum = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_num =
        generator.valueToCode(this, 'num', generator.ORDER_ATOMIC) || '1'

    return `sentry3_${dropdown_sentry_index}.SetParamNum(${dropdown_vision_obj},${input_num});\n`
}

export const Sentry3VisionColorSetParam = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')

    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC)
    var input_x =
        generator.valueToCode(this, 'x', generator.ORDER_ATOMIC)
    var input_y =
        generator.valueToCode(this, 'y', generator.ORDER_ATOMIC)
    var input_w =
        generator.valueToCode(this, 'w', generator.ORDER_ATOMIC)
    var input_h =
        generator.valueToCode(this, 'h', generator.ORDER_ATOMIC)

    generator.definitions_['param_obj'] = 'sentry_object_t param;\n'

    var code = '\n'
    code += `param.x_value = ${input_x};\n`
    code += `param.y_value = ${input_y};\n`
    code += `param.width = ${input_w};\n`
    code += `param.height = ${input_h};\n`
    code += `param.label = 0;\n`

    code += `sentry3_${dropdown_sentry_index}.SetParam(${dropdown_vision_obj},&param,${input_index});\n`

    return code
}

export const Sentry3VisionBlobSetParam = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')

    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC)
    var input_x = 0
    var input_y = 0
    var input_w =
        generator.valueToCode(this, 'w', generator.ORDER_ATOMIC)
    var input_h =
        generator.valueToCode(this, 'h', generator.ORDER_ATOMIC)

    var input_l = this.getFieldValue('lable')

    generator.definitions_['param_obj'] = 'sentry_object_t param;\n'

    var code = '\n'
    code += `param.x_value = ${input_x};\n`
    code += `param.y_value = ${input_y};\n`
    code += `param.width = ${input_w};\n`
    code += `param.height = ${input_h};\n`
    code += `param.label = ${input_l};\n`

    code += `sentry3_${dropdown_sentry_index}.SetParam(${dropdown_vision_obj},&param,${input_index});\n`

    return code
}
export const Sentry3VisionSetParam = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC)
    var input_x = 0
    var input_y = 0
    var input_w = 0
    var input_h = 0

    var input_l = this.getFieldValue('lable')

    generator.definitions_['param_obj'] = 'sentry_object_t param;\n'

    var code = '\n'
    code += `param.x_value = ${input_x};\n`
    code += `param.y_value = ${input_y};\n`
    code += `param.width = ${input_w};\n`
    code += `param.height = ${input_h};\n`
    code += `param.label = ${input_l};\n`

    code += `sentry3_${dropdown_sentry_index}.SetParam(${dropdown_vision_obj},&param,${input_index});\n`

    return code
}
export const Sentry3VisionSetBlobNum = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_mode = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC)

    var code = `sentry3_${dropdown_sentry_index}.VisionSetMode(${dropdown_vision_obj},${input_mode});\n`

    return code
}

export const Sentry3VisionSetAprilTagMode = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_mode = this.getFieldValue('mode')

    var code = `sentry3_${dropdown_sentry_index}.VisionSetMode(${dropdown_vision_obj},${input_mode});\n`

    return code
}

export const Sentry3VisionSetBarCodeMode = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_mode = this.getFieldValue('mode')

    var code = `sentry3_${dropdown_sentry_index}.VisionSetMode(${dropdown_vision_obj},${input_mode});\n`

    return code
}

export const Sentry3VisionSetLineNum = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_mode = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC)

    var code = `sentry3_${dropdown_sentry_index}.VisionSetMode(${dropdown_vision_obj},${input_mode});\n`

    return code
}

export const Sentry3LedSetColor = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var color_dic = {
        '#000000': 'kLedClose',
        '#ff0000': 'kLedRed',
        '#00ff00': 'kLedGreen',
        '#ffff00': 'kLedYellow',
        '#0000ff': 'kLedBlue',
        '#ff00ff': 'kLedPurple',
        '#00ffff': 'kLedCyan',
        '#ffffff': 'kLedWhite',
    }
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_led_color_obj1 =
        color_dic[this.getFieldValue('led_color_obj1')]
    var dropdown_led_color_obj2 =
        color_dic[this.getFieldValue('led_color_obj2')]

    var input_level = generator.valueToCode(this, 'level', generator.ORDER_ATOMIC) || 1

    return `sentry3_${dropdown_sentry_index}.LedSetColor(${dropdown_led_color_obj1},${dropdown_led_color_obj2},${input_level});\n`
}

export const Sentry3VisionDetectedCount = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')

    var code = `sentry3_${dropdown_sentry_index}.GetValue(${dropdown_vision_obj}, kStatus)`
    return [code, generator.ORDER_ATOMIC]
}


export const Sentry3GetVisionObjValue = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_res_obj = this.getFieldValue('vision_res_obj')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    var code = `sentry3_${dropdown_sentry_index}.GetValue(${dropdown_vision_obj},${dropdown_res_obj},${input_index})`
    return [code, generator.ORDER_ATOMIC]
}

export const Sentry3GetVisionObjLineValue = Sentry3GetVisionObjValue
export const Sentry3GetVisionObjStringValue = Sentry3GetVisionObjValue
export const Sentry3GetVisionObjColorValue = Sentry3GetVisionObjValue

export const Sentry3GetString = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC)
    var code = `String(sentry3_${dropdown_sentry_index}.GetString(${dropdown_vision_obj}, ${input_index}))`
    return [code, generator.ORDER_ATOMIC]
}


// 新的展开块生成器函数 - 直接检测视觉对象
export const Sentry3VisionDetectedColor = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_card_obj = this.getFieldValue('vision_card_obj')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    var code = `(sentry3_${dropdown_sentry_index}.GetValue(${dropdown_vision_obj},kLabel,${input_index})==${dropdown_card_obj})`
    return [code, generator.ORDER_ATOMIC]
}

export const Sentry3VisionDetectedBlob = Sentry3VisionDetectedColor
export const Sentry3VisionDetectedYolo = Sentry3VisionDetectedColor

export const Sentry3VisionDetectedHandPoseCustom = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    // 获取每个手指的状态：bent(弯曲=0), extended(张开=1), ignore(忽略=-1)
    var thumb_state = this.getFieldValue('thumb_state')
    var index_state = this.getFieldValue('index_state')
    var middle_state = this.getFieldValue('middle_state')
    var ring_state = this.getFieldValue('ring_state')
    var pinky_state = this.getFieldValue('pinky_state')

    // 计算mask和status
    // 位顺序：bit4(小拇指) bit3(无名指) bit2(中指) bit1(食指) bit0(大拇指)
    var mask = 0
    var status = 0

    // 大拇指 (bit 0)
    if (thumb_state !== 'ignore') {
        mask |= 0b00001
        if (thumb_state === 'extended') {
            status |= 0b00001
        }
    }

    // 食指 (bit 1)
    if (index_state !== 'ignore') {
        mask |= 0b00010
        if (index_state === 'extended') {
            status |= 0b00010
        }
    }

    // 中指 (bit 2)
    if (middle_state !== 'ignore') {
        mask |= 0b00100
        if (middle_state === 'extended') {
            status |= 0b00100
        }
    }

    // 无名指 (bit 3)
    if (ring_state !== 'ignore') {
        mask |= 0b01000
        if (ring_state === 'extended') {
            status |= 0b01000
        }
    }

    // 小拇指 (bit 4)
    if (pinky_state !== 'ignore') {
        mask |= 0b10000
        if (pinky_state === 'extended') {
            status |= 0b10000
        }
    }

    // 生成代码：((label & mask) == status)
    // 先获取label值
    var label_code = `sentry3_${dropdown_sentry_index}.GetValue(${dropdown_vision_obj},kLabel,${input_index})`
    // 将mask和status转换为5位二进制字符串
    var mask_bin = mask.toString(2)
    var mask_str = '0b' + ('00000' + mask_bin).slice(-5)
    var status_bin = status.toString(2)
    var status_str = '0b' + ('00000' + status_bin).slice(-5)
    var code = `((${label_code} & ${mask_str}) == ${status_str})`

    return [code, generator.ORDER_ATOMIC]
}

export const Sentry3VisionDetectedHandPosePreset = function (_, generator) {
    generator = generator ?? Blockly.Arduino
    var dropdown_sentry_index = this.getFieldValue('SentryIndex')
    var dropdown_vision_obj = this.getFieldValue('vision_obj')
    var dropdown_handpose_preset = this.getFieldValue('handpose_preset')
    var input_index =
        generator.valueToCode(this, 'index', generator.ORDER_ATOMIC) || '0'

    // 去除前缀大写字母（如 A0b00010 -> 0b00010）
    var preset_value = dropdown_handpose_preset.replace(/^[A-Z]/, '')

    // 预定义手势的值已经是二进制格式（如 0b00010）
    // 生成代码：((label & 0b11111) == preset_value)
    var label_code = `sentry3_${dropdown_sentry_index}.GetValue(${dropdown_vision_obj},kLabel,${input_index})`
    var code = `((${label_code} & 0b11111) == ${preset_value})`

    return [code, generator.ORDER_ATOMIC]
}